﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace DeviceUnion.FFmpeg.WinForm
{
    public partial class FFmpegCapture : Form
    {
        private FFRecorder player = new FFRecorder();
        private string RecorderDir { get; set; }
        public FFmpegCapture()
        {
            InitializeComponent();
            this.Text += " - okok";
            player.OnCmdOutput += Player_OnWriteInfo;
            var dir = AppDomain.CurrentDomain.BaseDirectory;
            RecorderDir = dir;
            player.Open();
        }

        private void Player_OnWriteInfo(object sender, LogEventArgs e)
        {
            if (this.richTextBox1_log.InvokeRequired)
            {
                this.richTextBox1_log.Invoke(new Action(() =>
                {
                    Player_OnWriteInfo(sender, e);
                }));
            }
            else
            {
                this.richTextBox1_log.AppendText(e.Text + "\n");
            }
        }

        /// <summary>
        /// 录制桌面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            var dir = AppDomain.CurrentDomain.BaseDirectory;
            var f = string.Format("桌面-{0}.mp4", DateTime.Now.ToString("yyyyMMddhhmmss"));
            f = System.IO.Path.Combine(dir, f);
            var p = new FFParam()
            {
                VideoCodec= VCodec.libx264,
                OutFile = f,
                //Duration = 30,
                FrameRate = 10,
                Rate = 10,
                VideoSize = new Size(1920, 1080),
                VideoOffSet = new Point(0, 0),
                Mute = true,
                LogLevel = "32",
                WaterMarkText = new WaterMark()
                {
                    FontColor = "red",
                    FontFile = "msyh.ttc",
                    FontSize = 25,
                    UseTimeReplaceText = true,
                    XLocation = "w-tw-10",
                    YLocation = "60",
                    Tag = @":tc24hmax=1 ",
                    IsEmpty = true,
                },
            };
            player.StartRecordDesktop(p);
        }

        /// <summary>
        /// 停止录制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            player.StopRecord();
        }

        /// <summary>
        /// 打开录屏文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(RecorderDir);
        }

        /// <summary>
        /// 录制当前窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            var dir = AppDomain.CurrentDomain.BaseDirectory;
            var f = string.Format("窗口-{0}-{1}.mp4", this.Text, DateTime.Now.ToString("yyyyMMddhhmmss"));
            f = System.IO.Path.Combine(dir, f);
            var parm = new FFmpeg.WinForm.FFParam()
            {
                Window = this.Text ,
                OutFile = f,
                Duration = 30,
                FrameRate = 10,
                Rate = 10,
                Mute = true,
                LogLevel = "32",
                WaterMarkText = new WaterMark()
                {
                    FontColor = "red",
                    FontFile = "msyh.ttc",
                    FontSize = 25,
                    Text = "中文水印",
                    XLocation = "w-tw-10",
                    YLocation = "60",
                    IsEmpty = true,
                },
            };
            player.StartRecordWindow(parm);
        }

        /// <summary>
        /// 录制指定窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button7_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
                return;
            var dir = AppDomain.CurrentDomain.BaseDirectory;
            var f = string.Format("窗口-{0}-{1}.mp4", this.textBox1.Text, DateTime.Now.ToString("yyyyMMddhhmmss"));
            f = System.IO.Path.Combine(dir, f);
            player.StartRecordWindow(this.textBox1.Text, f);
        }

        /// <summary>
        /// 将mp4转换为gif
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button8_Click(object sender, EventArgs e)
        {
            player.ConvertTOGif(this.textBox3.Text,this.textBox2.Text, (int)this.numericUpDown1.Value);
        }

        /// <summary>
        /// 选择要保存的gif文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var f = new SaveFileDialog();
            f.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            f.Filter = "(*.gif)|*.gif";
            f.DefaultExt = ".gif";
            if (f.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = f.FileName;
            }
        }
        /// <summary>
        /// 选择要转换的mp4文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var f = new OpenFileDialog();
            f.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            f.Filter = "(*.mp4)|*.mp4";
            f.Multiselect = false;
            if(f.ShowDialog()== DialogResult.OK)
            {
                this.textBox3.Text = f.FileName;
            }
        }
    }
}
